/**
 * 部门树形下拉菜单
 * 使用说明:
 js:  $(function(){
            $("#catalog_name").catalogTreeMenu();
       })
 html:
 <div class="form-group">
 <label class="col-sm-2 control-label">目录：</label>
 <div class="col-sm-4">
 <input class="form-control" type="text" id="catalog_name" name="catalogName" ztree="catalog" readonly>
 <input type="hidden" id="catalog_id" name="catalogId">
 </div>
 </div>
 *
 */
;(function($) {
    $.fn.catalogTreeMenu = function(){
        var input = $(this).attr("ztree");
        var treeMenu = input+"_treeMenu";
        var treeMenuDiv = input+"_treeMenu_div";
        var div = '<div id="'+treeMenuDiv+'" class="ztreemenu" style="display:none; position: absolute;z-index: 1888;">';
        div+='<ul id="'+treeMenu+'" class="ztree" style="margin-top:0;height:250px;overflow: auto;"></ul></div>';
        $("#"+input+"_id").after(div);
        var setting = {
            view: {
                dblClickExpand:false
            },
            data:{
                simpleData:{
                    enable: true
                }
            },
            callback: {
                onClick: function(e, treeId, treeNode){
                    var zTree = $.fn.zTree.getZTreeObj(treeId);
                    var nodes = zTree.getSelectedNodes();
                    var nodeId = nodes[0].id;
                    var nodeName = nodes[0].name;
                    //////////// 只对选择的课程有效oricatalog_treeMenu.
                    if(treeId.indexOf("oricatalog") >= 0) {
                        var oricatalogId = nodeId;
                        var selectedCourseIds = getSelectIds("ckSub");
                        $.post("/lrm/controller/course/checkCourseContainCatalog", {
                            "oricatalogId": oricatalogId,
                            "selectedCourseIds": selectedCourseIds
                        }, function (data) {
                            var message = data.message;
                            if (message == "error") {
                                BootstrapDialog.alert({title: '信息', message: '存在课程不包含目录['+nodes[0].name+']！', buttonLabel: "确定"});
                            } else {
                                $("#" + input + "_id").val(nodes[0].id);
                                $("#" + input + "_name").val(nodes[0].name);
                            }
                        });
                    } else {
                        $("#" + input + "_id").val(nodes[0].id);
                        $("#" + input + "_name").val(nodes[0].name);
                    }
                    hideMenu();
                    zTree.cancelSelectedNode();//取消节点选中状态
                }
            }
        };

        $.post("/lrm/controller/catalog/getCatalogList",function(data){
            var catalogList = data.catalogList;
            var companyName = data.companyName;
            var zNodes = "[{ id:'0', pId:'0', name:companyName, open:true, noRemoveBtn: true, noRenameBtn: true}";
            $.each(catalogList,function(i,catalog){
                var id = catalog.catalogId;
                var pId = catalog.parentCatalogId==''?'0': catalog.parentCatalogId;
                var name = catalog.catalogName;
                var orderNo = catalog.catalogOrderNo;
                zNodes+=",{id: '"+id+"', pId: '"+pId+"', name: '"+name+"', orderNo: "+orderNo+", open: true}";
            });
            zNodes+="]";
            $.fn.zTree.init($("#"+treeMenu), setting, eval(zNodes));
        });


        var hideMenu = function(){
            $("#"+treeMenuDiv).fadeOut("fast");
            $("body").unbind("mousedown", onBodyDown);
        };

        var onBodyDown = function(event){
            if (!(event.target.id == "menuBtn" || event.target.id == treeMenuDiv || $(event.target).parents("#"+treeMenuDiv).length>0)) {
                hideMenu();
            }
        };

        $(this).click(function(){
            $("#"+treeMenuDiv).css({left:15+"px", top:30 + "px",width:$(this).width()+26+"px"}).slideDown("fast");
            $("body").bind("mousedown", onBodyDown);
        })
    };
})(jQuery);